package com.qsmx.store.app.main.goods.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.qsmx.store.app.R;
import com.qsmx.store.app.main.goods.entity.GoodsEntity;
import com.qsmx.store.app.main.goods.holder.GoodsListHolder;

import java.util.List;

public class GoodsListAdapter extends RecyclerView.Adapter<GoodsListHolder> {

    private Context mContext;

    private List<GoodsEntity> mData;

    //定义接口
    public static interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    //声明自定义的监听接口
    private GoodsListAdapter.OnItemClickListener monItemClickListener = null;

    //提供set方法
    public void setonItemClickListener(GoodsListAdapter.OnItemClickListener listener) {
        this.monItemClickListener = listener;
    }

    public void setData(List<GoodsEntity> mData) {
        this.mData = mData;
    }

    public GoodsListAdapter(Context mContext) {
        this.mContext = mContext;
    }

    @NonNull
    @Override
    public GoodsListHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new GoodsListHolder(LayoutInflater.from(mContext).inflate(R.layout.adapter_goods_list, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull GoodsListHolder goodsListHolder, final int position) {
        Glide.with(mContext).load(mData.get(position).getGoodsImageUrl()).into(goodsListHolder.ivImg);
        goodsListHolder.tvGoodsName.setText(mData.get(position).getGoodsName());
        goodsListHolder.tvMoney.setText(String.format(mContext.getString(R.string.goods_price_info), String.valueOf(mData.get(position).getPrice())));
        goodsListHolder.tvInventory.setText(String.format(mContext.getString(R.string.goods_inventory_info), String.valueOf(mData.get(position).getInventory())));
        goodsListHolder.tvSku.setText(String.format(mContext.getString(R.string.goods_sku_info), String.valueOf(mData.get(position).getGoodsSku())));

        goodsListHolder.clContent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                monItemClickListener.onItemClick(v, position);
            }
        });

    }

    @Override
    public int getItemCount() {
        if (mData != null && mData.size() > 0) {
            return mData.size();
        } else {
            return 0;
        }
    }
}
